## Figure 1

#directory
#set working directory to the path PvP_Replication
#setwd("~/PvP_Replication")

#packages
library(tidyverse)

#create empty month-year time series
chronology_df <- expand.grid(
  monthyear = seq(as_date(mdy('January, 2016')), as_date(mdy('January, 2020')), by="month"),
  foundertier = c("top", "middle", "low"))

#read data on splinter groups
splinter_groups <- read.csv("PvP_data/splinter_groups.csv")

#aggregate splinter group data to monthly count of splinter groups
splinter_groups <- splinter_groups %>% 
  mutate(monthyear = paste(month, year, sep = ", ")) %>% 
  group_by(foundertier, monthyear) %>% 
  summarise(n_groups = n()) %>% 
  mutate(monthyear = as_date(mdy(monthyear))) %>% 
  dplyr::select(monthyear, foundertier, n_groups)

#join data on splinter groups to time series
chronology_df <- left_join(chronology_df, splinter_groups, by = c("foundertier", "monthyear"))

#NAs are real zeros so convert 
chronology_df <- chronology_df %>% mutate(n_groups = ifelse(is.na(n_groups), 0, n_groups))

#generate cumulative sum of splinter groups
chronology_df <- chronology_df %>% 
  group_by(foundertier) %>% 
  arrange(monthyear) %>% 
  mutate(sum_groups = cumsum(n_groups))

#plot
chronology_plot <- ggplot() + 
  geom_area(aes(y = sum_groups, x = monthyear, fill = foundertier), data = chronology_df, stat="identity") + 
  scale_fill_manual(values =  c("#0072B2", "#D55E00", "#009E73")) + 
  labs(y = "FARC Splinter Groups (count)", x = "", fill = "Rank of \nFounding \nCommander") + 
  theme_minimal()

#save plot to folder
ggsave(plot = chronology_plot, filename="PvP_plots/chronology_plot.pdf", width = 8,
       height = 4, units = "in")

